//
// Created by yuping.li on 10/29/24.
//

#ifndef USER_INPUT_TASK_HPP_
#define USER_INPUT_TASK_HPP_

#include "state_machine/fsm_incubatorfsm.h"
#include "task.hpp"
#include "usart.h"

class UserInputTask : public Task {
 public:
  UserInputTask() {
    period_ms = 50U;
  }

  void init_task() override {
    m_incubator_fsm = std::make_unique<IncubatorFsm>();
    m_incubator_fsm->start();
  }

  void period_task() override {
    static uint8_t data{};
    auto ret = HAL_UART_Receive(&huart1, &data, 1, 1);
    if (ret != HAL_OK) {
      return;
    }
    if (data == 0x0d) { //Enter
      m_incubator_fsm->dispatch(m_incubator_fsm->key_enter);
    }

    if (data == 0x64) { //d
      m_incubator_fsm->dispatch(m_incubator_fsm->key_switch);
    }

    if (Rte::instance().incubator_mode == MODE_CUSTOMER) {
      if ((data == 0x77) && Rte::instance().target_temp < 400) { //w
        ++Rte::instance().target_temp;
      } else if ((data == 0x73) && Rte::instance().target_temp > 0) { //s
        --Rte::instance().target_temp;
      }
    }

  }

 protected:
  std::unique_ptr<IncubatorFsm> m_incubator_fsm{nullptr};
};

#endif //USER_INPUT_TASK_HPP_
